<?
/**
 * Copyright 2007 Melange.
 *
 * This file is part of PHP-MELANGE.
 *
 * PHP-MELANGE is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PHP-MELANGE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHP-MELANGE; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category    Melange
 * @package     php-melange
 * @subpackage  cms
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 *
 */

require_once("mvc/action/Action.php");

class AbstractListAction extends Action {

    protected $errors;
    protected $messages;

    public function execute(ActionConfig &$mapping, &$form, Request &$request,
        Response &$response) {

        $action =& $form->getAction();
        
        // Nothing to do. return.
        if(strlen($action) == 0)
            return $mapping->findForward("success");

        $this->messages = new ActionMessages();
        $this->errors   = new ActionMessages();

        // Transaction check
        if (!$this->isTokenValid($request)) {
            $form->reset();
            $this->errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("error.transaction", true));
            $this->saveErrors($request, $this->errors);
            return $mapping->getInputForward();
        }

        $this->resetToken($request);

        //treat action as methodname to call instead of execute
        $method = $action;
        if(method_exists($this, $method)) {
            call_user_func(
                array(&$this, $method), $mapping, $form, $request, $response);
        } else {
            $this->unspecified($action);
        }

        if(!$this->errors->isEmpty()) {
            $this->saveErrors($request, $this->errors);
            return $mapping->getInputForward();
        }

        if(!$this->messages->isEmpty())
            $this->saveMessages($request, $this->messages);

        $form->reset();

        return $mapping->findForward("success");

    }

    protected function unspecified($action) {
        throw new Exception("Unspecified method '${action}'");
    }

}
?>